package jps.result;

import java.util.ArrayList;
import java.util.Collection;

import edu.pjwstk.jps.result.IAbstractQueryResult;
import edu.pjwstk.jps.result.IBagResult;
import edu.pjwstk.jps.result.ISingleResult;

public class BagResult extends CollectionResult implements IBagResult {
	private Collection<ISingleResult> collection;
	private ArrayList<ISingleResult> arrayList;
	
	public BagResult(ISingleResult result){
		arrayList = new ArrayList<>();
		arrayList.add((ISingleResult)result);
		this.collection = arrayList;
	}
	
	public BagResult(Collection<ISingleResult> collection) {
		super();
		this.collection = collection;
	}

	public BagResult() {
		arrayList = new ArrayList<>();
		// TODO Auto-generated constructor stub
	}

	@Override
	public Collection<ISingleResult> getElements() {
		// TODO Auto-generated method stub
		return collection;
	}

	public ISingleResult getElement(int i){
		arrayList = (ArrayList<ISingleResult>)this.collection;
		return arrayList.get(i);
		
	}
	public Integer getSize(){
		arrayList = (ArrayList<ISingleResult>)this.collection;
		return arrayList.size();
		
	}
	public void addElements(Collection <ISingleResult> elements)
	{
		
		this.collection.addAll(elements);
	}
	@Override
	public String toString() {
		return "\n[BagResult]\n  [" + collection + "\n[/BagResult]";
	}

	 public void add(IAbstractQueryResult res) {
         if(res instanceof ISingleResult) {
        	 this.arrayList.add((ISingleResult) res);
        	 this.collection = arrayList;
         } else if(res instanceof IBagResult) {
                 this.arrayList.addAll(((IBagResult) res).getElements());
                 this.collection = arrayList;
         }
 }

}
